import { createStore } from 'vuex'
import router from '../router';
export default createStore({
  state: {
    users:[]
  },
  getters:{
    getmy:(state)=>(a)=>{
      state.users = JSON.parse(localStorage.getItem('users'));
      let exit = state.users.find(item=>item.mobile==a._value);
      if(!exit){
        router.push("/login")
      }
      return exit;
    }
  },
  mutations: {
    getusers (state,a) {
      if(!JSON.parse(localStorage.getItem('users'))){
        localStorage.setItem('users',[])
      }else{
        state.users = JSON.parse(localStorage.getItem('users'));
      }
      let exit = state.users.findIndex(item=>item.mobile==a.mobile);
      if(exit==-1){
        alert("该用户不存在,请注册")
      }
      else{
        if(a.password==state.users[exit].password){
          alert("登录成功")
          localStorage.setItem('my',JSON.stringify(a.mobile))
          router.push("/")
        }
        else{
          alert('密码错误')
        }
      }
    },
    setusers(state,a){
      if(!JSON.parse(localStorage.getItem('users'))){
        localStorage.setItem('users',[])
      }else{
        state.users = JSON.parse(localStorage.getItem('users'));
      }
      let exit = state.users.some(item=>item.mobile==a.mobile);
      if(exit){
        alert("该用户已注册")
      }
      else{
        state.users = [...state.users,a]
        localStorage.setItem('users',JSON.stringify(state.users))
        alert("注册成功");
        router.push('/login');
      }
    },
  },
  actions: {
  },
  modules: {
  }
})